export default {
  // 为当前模块开启命名空间
  namespaced: true,

  // 模块的 state 数据
  state: () => ({
  cart: uni.getStorageSync('cart') || [] // 购物车商品列表
  }),

  // 模块的 mutations 方法
  mutations: {
    // 持久化数据
    saveToStorage(state){
      uni.setStorageSync('cart', state.cart)
    },
    addToCart(state, goods){
      let f = state.cart.find(t => t.goods_id === goods.goods_id)
      if(f){
        f.goods_count++
      }else{
        state.cart.push(goods)
      }
      this.commit('m_cart/saveToStorage')
    },
    updateGoodsState(state,goods){
        let f = state.cart.find(t=> t.goods_id === goods.goods_id)
        if(f){
          f.goods_state = goods.goods_state
          this.commit('m_cart/saveToStorage')
        }
    }
  },

  // 模块的 getters 属性
  getters: {
    total(state){
      let result = 0
      state.cart.forEach(t => result += t.goods_count )
      return result
    }
  },
}
